package by.averianov.discounta.webapp.page;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Page;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeAction;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.http.handler.RedirectRequestHandler;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.webapp.app.BasicAuthenticationSession;
import by.averianov.discounta.webapp.app.WicketWebApplication;
import by.averianov.discounta.webapp.page.administrator.Administrator;
import by.averianov.discounta.webapp.page.homepage.HomePage;
import by.averianov.discounta.webapp.page.homepage.locale.LanguageSelectionPanel;
import by.averianov.discounta.webapp.page.loginpage.LoginPage;
import by.averianov.discounta.webapp.page.pricebook.Economist;
import by.averianov.discounta.webapp.page.shop.InShop;
import by.averianov.discounta.webapp.page.user.UserProfilePage;
import by.averianov.discounta.webapp.page.viewsale.ViewSale;

public abstract class BaseLayout extends WebPage implements Serializable {
	
	public String currentMenu;
	public SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	public BaseLayout(PageParameters pageParameters) {
		super();
	}

	public String getCurrentId() {
		return currentMenu;
	}

	public void setCurrentId(String currentId) {
		this.currentMenu = currentId;
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		add(new Label("headerTitle", getPageTitle()));
		add(new Label("labelAboutLogo", getAboutLogo()));
		
		Link logo = new Link("logo"){
			@Override
			public void onClick() {
				setResponsePage(new HomePage());
			}
		};
		if (currentMenu == "home") {
			logo.add(AttributeModifier.append("class", "disabled"));
		}
		logo.add(new Label("labelLogo", getLogo()));
		add(logo);
		
		Link home = new Link("home"){
			@Override
			public void onClick() {
				setResponsePage(new HomePage());
			}
		};
		if (currentMenu == "home") {
			home.add(AttributeModifier.append("class", "disabled"));
			home.add(AttributeModifier.append("class", "current"));
		}
		home.add(new Label("labelHome", getMenuHome()));
		add(home);
	
		Link viewSale = new SecuredLinkForSpokesman("viewSale"){
			@Override
			public void onClick() {
				setResponsePage(new ViewSale(new PageCreator() {

					@Override
					public Page createPage() {
						return new HomePage();
					}
				}));
			}
		};
		if (currentMenu == "viewSale") {
			viewSale.add(AttributeModifier.append("class", "disabled"));
			viewSale.add(AttributeModifier.append("class", "current"));
		}
		viewSale.add(new Label("labelViewSale", getMenuViewSale()));
		add(viewSale);
		
		Link priceBook = new SecuredLinkForEcon("priceBook"){
			@Override
			public void onClick() {
				setResponsePage(new Economist(0, 0L));
			}
		};
		if (currentMenu == "priceBook") {
			priceBook.add(AttributeModifier.append("class", "disabled"));
			priceBook.add(AttributeModifier.append("class", "current"));
		}
		priceBook.add(new Label("labelPriceBook", getMenuPriceBook()));
		add(priceBook);
		
		Link administrator = new SecuredLinkForAdmin("administrator"){
			@Override
			public void onClick() {
				setResponsePage(new Administrator(0, 0L));
			}
		};
		if (currentMenu == "administrator") {
			administrator.add(AttributeModifier.append("class", "disabled"));
			administrator.add(AttributeModifier.append("class", "current"));
		}
		add(administrator);
				
		Link shop = new Link("shop"){
			@Override
			public void onClick() {
				setResponsePage(new InShop(0L, 0L));
			}
		};
		if (currentMenu == "shop") {
			shop.add(AttributeModifier.append("class", "disabled"));
			shop.add(AttributeModifier.append("class", "current"));
		}
		shop.add(new Label("labelShop", getMenuShop()));
		add(shop);
		
		Link menuProfile = new SecuredLinkForAll("profile"){
			@Override
			public void onClick() {
				//setResponsePage(new UserProfilePage());
				
				setResponsePage(new UserProfilePage(new PageCreator() {

					@Override
					public Page createPage() {
						return new HomePage();
					}
				}));
			}
		};
		if (currentMenu == "profile") {
			menuProfile.add(AttributeModifier.append("class", "disabled"));
			menuProfile.add(AttributeModifier.append("class", "current"));
		}
		menuProfile.add(new Label("labelProfile", getMenuProfile()));
		add(menuProfile);
		
		Link menuLogin = new Link("menuLogin"){
			@Override
			protected void onConfigure() {
				super.onConfigure();
				//boolean isLoginPage = LoginPage.class.equals(getPage().getClass());
				//boolean isLogged = BasicAuthenticationSession.get().isSignedIn();

				//setVisible(!(isLogged || isLoginPage));
				setVisible(!BasicAuthenticationSession.get().isSignedIn());
			}
			
			@Override
			public void onClick() {
				
				setResponsePage(new LoginPage(null));
				
				/*setResponsePage(new LoginPage(new PageCreator() {

					@Override
					public Page createPage() {
						return new HomePage();
					}
				}));*/
			}
		};
		if (currentMenu == "menuLogin") {
			menuLogin.add(AttributeModifier.append("class", "disabled"));
			menuLogin.add(AttributeModifier.append("class", "current"));
		}
		menuLogin.add(new Label("labelLogin", getMenuLogin()));
		add(menuLogin);
		
		Link menuLogout = new Link("menuLogout"){		
			@Override
			protected void onConfigure() {
				super.onConfigure();
				setVisible(BasicAuthenticationSession.get().isSignedIn());
			}
			
			@Override
			public void onClick() {
				final HttpServletRequest servletReq = (HttpServletRequest) getRequest().getContainerRequest();
				servletReq.getSession().invalidate();
				getSession().invalidate();
				getRequestCycle().scheduleRequestHandlerAfterCurrent(new RedirectRequestHandler(WicketWebApplication.LOGIN_URL));
				setResponsePage(new HomePage());
			}
		};
				
		/*if (currentId == "menuLogin") {
			menuLogout.add(AttributeModifier.append("class", "disabled"));
			menuLogout.add(AttributeModifier.append("class", "current"));
		}*/
		menuLogout.add(new Label("labelLogout", getMenuLogout()));
		add(menuLogout);

		UserProfile userProfile = BasicAuthenticationSession.get().getUserProfile();
		add(new Label("userName", new Model(userProfile != null ? userProfile.getUserFirstName() + " " + userProfile.getUserLastName() : null)));
		
		add(new FeedbackPanel("feedbackpanel"));
		
		add(new LanguageSelectionPanel("lang-select"));
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "user", "administrator", "economist", "spokesman" })
	protected abstract class SecuredLinkForAll extends Link<Void> {

		public SecuredLinkForAll(String id) {
			super(id);
		}
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "spokesman" })
	protected abstract class SecuredLinkForSpokesman extends Link<Void> {

		public SecuredLinkForSpokesman(String id) {
			super(id);
		}
	}	
	
	@AuthorizeAction(action = Action.RENDER, roles = { "administrator" })
	protected abstract class SecuredLinkForAdmin extends Link<Void> {

		public SecuredLinkForAdmin(String id) {
			super(id);
		}
	}	

	@AuthorizeAction(action = Action.RENDER, roles = { "economist" })
	protected abstract class SecuredLinkForEcon extends Link<Void> {

		public SecuredLinkForEcon(String id) {
			super(id);
		}
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "administrator" })
	protected abstract class SecuredLabelForAdmin extends Label {

		public SecuredLabelForAdmin(String id, IModel<?> model) {
			super(id, model);
		}
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "administrator" })
	protected abstract class SecuredDropDownChoiceForAdmin<T> extends DropDownChoice<T> {
		
		public SecuredDropDownChoiceForAdmin(final String id, IModel<T> model, final List<? extends T> choices)
		{
			super(id, model, choices);
		}
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "administrator" })
	protected abstract class SecuredAdminTextFieldAdmin<T> extends TextField<T> {
		
		public SecuredAdminTextFieldAdmin(String id, IModel<T> model) 
		{
			super(id, model);
		}
	}
	
	@AuthorizeAction(action = Action.RENDER, roles = { "administrator" })
	protected abstract class SecuredAdminCheckBoxAdmin extends CheckBox {

		public SecuredAdminCheckBoxAdmin(String id, IModel<Boolean> model) 
		{
			super(id, model);
		}

	}
	
	protected IModel<String> getButtonAdd() {
		return new ResourceModel("p.button.add");
	}
	
	protected IModel<String> getButtonAddProduct() {
		return new ResourceModel("p.shop.product.button.addproduct");
	}
	protected IModel<String> getButtonToShopCart() {
		return new ResourceModel("p.shop.product.button.toshopcart");
	}
	
	protected IModel<String> getButtonChange() {
		return new ResourceModel("p.button.change");
	}

	protected IModel<String> getButtonDel() {
		return new ResourceModel("p.button.del");
	}
	
	protected IModel<String> getButtonSave() {
		return new ResourceModel("p.button.save");
	}
	
	protected IModel<String> getButtonCancel() {
		return new ResourceModel("p.button.cancel");
	}
	
	protected IModel<String> getButtonEnter() {
		return new ResourceModel("p.button.enter");
	}
	
	protected IModel<String> getLabelShopId() {
		return new ResourceModel("p.createshop.id");
	}
	
	protected IModel<String> getLabelCreateShop(Long id) {
		if(id == null) {
			return new ResourceModel("p.createshop.createshop");
		} else {
			return new ResourceModel("p.createshop.changeshop");
		}
	}

	protected IModel<String> getDNS() {
		return new ResourceModel("dns");
	}
	
	protected IModel<String> getPort() {
		return new ResourceModel("port");
	}

	protected IModel<String> getEmailTemplate1() {
		return new ResourceModel("p.email.template1");
	}

	protected IModel<String> getEmailTemplate2() {
		return new ResourceModel("p.email.template2");
	}

	protected IModel<String> getEmailTemplate3() {
		return new ResourceModel("p.email.template3");
	}
	
	protected IModel<String> getEmailTemplate4() {
		return new ResourceModel("p.email.template4");
	}
	
	protected IModel<String> getLabelChangeShop() {
		return new ResourceModel("p.createshop.changeshop");
	}
	
	protected IModel<String> getLabelNameShop() {
		return new ResourceModel("p.createshop.nameshop");
	}

	protected IModel<String> getLabelAbout() {
		return new ResourceModel("p.home.about");
	}
	
	protected IModel<String> getLabelTitle() {
		return new ResourceModel("p.base.title");
	}
	
	protected IModel<String> getLabelEconomistDiscount() {		
		return new ResourceModel("p.economist.discount");
	}
	
	protected IModel<String> getLabelEconomistReports() {		
		return new ResourceModel("p.economist.reports");
	}

	protected IModel<String> getLabelAdministratorShops() {		
		return new ResourceModel("p.administrator.shops");
	}
	
	protected IModel<String> getLabelAdministratorCards() {		
		return new ResourceModel("p.administrator.cards");
	}
	
	protected IModel<String> getLabelAdministratorCardTypes() {		
		return new ResourceModel("p.administrator.cardtypes");
	}
	
	protected IModel<String> getLabelAdministratorUsers() {		
		return new ResourceModel("p.administrator.users");
	}
	
	protected IModel<String> getLabelCreateUser(Long id) {	
		if(id == null) {
			return new ResourceModel("p.createuser.createuser");
		} else {
			return new ResourceModel("p.createuser.changeuser");
		}
	}
	
	protected IModel<String> getLabelCreatePriceBook(Long id) {	
		if(id == null) {
			return new ResourceModel("p.createpricebook.createpricebook");
		} else {
			return new ResourceModel("p.createpricebook.changepricebook");
		}
	}
	
	protected IModel<String> getLabelCreateCard(Long id) {	
		if(id == null) {
			return new ResourceModel("p.createcard.createcard");
		} else {
			return new ResourceModel("p.createcard.changecard");
		}
	}
	
	protected IModel<String> getLabelCreateCardType(Long id) {	
		if(id == null) {
			return new ResourceModel("p.createcardtype.createcardtype");
		} else {
			return new ResourceModel("p.createcardtype.changecardtype");
		}
	}

	protected IModel<String> getLabelUserIdText() {
		return new ResourceModel("p.createuser.id");
	}
	
	protected IModel<String> getLabelUserStatus() {
		return new ResourceModel("p.createuser.userstatus");
	}
	
	protected IModel<String> getLabelActivateCode() {
		return new ResourceModel("p.createuser.activatecode");
	}
	
	protected IModel<String> getLabelCardTypeId() {
		return new ResourceModel("p.createcardtype.id");
	}
	
	protected IModel<String> getLabelPriceBookIdText() {
		return new ResourceModel("p.createpricebook.id");
	}
	
	protected IModel<String> getLabelCardIdText() {
		return new ResourceModel("p.createcard.id");
	}
	
	protected IModel<String> getLabelNameCardType() {
		return new ResourceModel("p.createcardtype.namecardtype");
	}
	
	/*protected IModel<String> getNoCardType() {
		return new ResourceModel("p.createcard.nocardtype");
	}*/
	
	protected IModel<String> getNoUsersLogin() {
		return new ResourceModel("p.createcard.nouserlogin");
	}
	
	protected IModel<String> getLabelPassword() {
		return new ResourceModel("p.createuser.password");
	}

	protected IModel<String> getLabelEmail() {
		return new ResourceModel("p.createuser.email");
	}

	protected IModel<String> getLabelPhoneNumber() {
		return new ResourceModel("p.createuser.phonenumber");
	}

	protected IModel<String> getLabelUserLastName() {
		return new ResourceModel("p.createuser.userlastname");
	}

	protected IModel<String> getLabelUserFirstName() {
		return new ResourceModel("p.createuser.userfirstname");
	}

	protected IModel<String> getLabelAccessGroup() {
		return new ResourceModel("p.createuser.accessgroup");
	}

	protected IModel<String> getLogo() {
		return new ResourceModel("p.base.title");
	}
	
	protected IModel<String> getAboutLogo() {
		return new ResourceModel("p.base.title.about");
	}

	protected IModel<String> getMenuHome() {
		return new ResourceModel("p.base.menu.home");
	}

	protected IModel<String> getMenuViewSale() {
		return new ResourceModel("p.base.menu.viewsale");
	}

	protected IModel<String> getMenuPriceBook() {
		return new ResourceModel("p.base.menu.pricebook");
	}
	
	protected IModel<String> getMenuAdministrator() {
		return new ResourceModel("p.base.menu.administrator");
	}

	protected IModel<String> getMenuShop() {
		return new ResourceModel("p.base.menu.shop");
	}


	protected IModel<String> getMenuProfile() {
		return new ResourceModel("p.base.menu.profile");
	}

	protected IModel<String> getMenuLogin() {
		return new ResourceModel("p.base.menu.login");
	}
	
	protected IModel<String> getMenuLogout() {
		return new ResourceModel("p.base.menu.logout");
	}
	
	protected IModel<String> getButtonActivate() {
		return new ResourceModel("p.home.button.activate");
	}

	protected IModel<String> getButtonCreateUser() {
		return new ResourceModel("p.home.button.createuser");
	}

	protected IModel<String> getTextButtonCreateUser() {
		return new ResourceModel("p.home.button.createusertext");
	}
	
	protected IModel<String> getTextButtonLogin() {
		return new ResourceModel("p.home.button.logintext");
	}
	
	protected IModel<String> getButtonAddCard() {
		return new ResourceModel("p.home.button.addcard");
	}

	protected IModel<String> getTextButtonAddCard() {
		return new ResourceModel("p.home.button.addcardtext");
	}
	
	protected IModel<String> getPageTitle() {
		return new Model<String>(getClass().getSimpleName());
	}

}